package net.oschina.app.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.oschina.app.R;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.bean.BlogBean;
import net.oschina.app.util.CommonUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wei03 on 2017/1/14.
 */
public class BaseNewsBLogAdapter extends BaseAdapter {
    @Override
    public NewsBlogHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemtView = CommonUtils.getInflate().inflate(R.layout.item_rv_news, parent, false);
        NewsBlogHolder newsInfosHolder = new NewsBlogHolder(itemtView);
        return newsInfosHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NewsBlogHolder newsInfosHolder = (NewsBlogHolder) holder;
        BlogBean blogBean = (BlogBean) mDatas.get(position);
        SharedPreferences sp = CommonUtils.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);

        int id = sp.getInt(String.valueOf(blogBean.getId()), -1);
        if (id == blogBean.getId()) {
            newsInfosHolder.mTvTitleSynthesize.setTextColor(Color.GRAY);
        } else {
            newsInfosHolder.mTvTitleSynthesize.setTextColor(Color.BLACK);
        }
        newsInfosHolder.mTvAuthorSynthesize.setText(blogBean.getAuthorname());
        if (blogBean.getBody() != null) {
            newsInfosHolder.mTvContentSynthesize.setText(Html.fromHtml(blogBean.getBody()));
        } else {
            newsInfosHolder.mTvContentSynthesize.setText("");
        }
        newsInfosHolder.mTvTitleSynthesize.setText(blogBean.getTitle());
        newsInfosHolder.mTvDataSynthesize.setText(blogBean.getPubDate());
        newsInfosHolder.mTvDiscussSynthesize.setText(String.valueOf(blogBean.getCommentCount()));
        newsInfosHolder.itemView.setTag(blogBean.getId());
    }

    public class NewsBlogHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title_synthesize)
        public TextView mTvTitleSynthesize;
        @BindView(R.id.tv_content_synthesize)
        TextView mTvContentSynthesize;
        @BindView(R.id.tv_author_synthesize)
        TextView mTvAuthorSynthesize;
        @BindView(R.id.tv_data_synthesize)
        TextView mTvDataSynthesize;
        @BindView(R.id.tv_discuss_synthesize)
        TextView mTvDiscussSynthesize;

        NewsBlogHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
